﻿using System.Collections.Generic;
using DFGK.IService.BBS;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Model.Models.BBS;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.Platform
{
    /// <summary>
    /// 公告信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BBS_PostController : BaseController
    {
        private readonly IBBS_PostService _postService;

        public BBS_PostController(IBBS_PostService postService, IWebHostEnvironment webEnviroment) : base(webEnviroment)
        {
            _postService = postService;
        }

        /// <summary>
        /// 公告分页查询
        /// </summary>
        /// <param name="title">标贴</param>
        /// <param name="status">公告状态</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        [HttpGet]
        public IActionResult GetPostPageData(string? title, int? status, int pageIndex = 1, int pageSize = 50)
        {   
            int totalCount;
            var pageData = _postService.GetPageData(title, status, pageIndex, pageSize, out totalCount);
            return Ok(BaseResult.Result(pageData, "查询成功！", "200", totalCount));
        }

        /// <summary>
        /// 新增/修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SubmitPost(BBS_Post model)
        {
            if (model.Id > 0)
            {
                model.ModifyUserId = base.BaseGetNowUserId();
                model.ModifyUserName = base.BaseGetNowUserName();
            }
            else
            {
                model.CreateUserId = base.BaseGetNowUserId();
                model.CreateUserName = base.BaseGetNowUserName();
            }
            bool result = _postService.SubmitPostData(model, out string message);
            if (result)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 公告状态 0-未发布 1-已发布
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult UpdateStatus(long id)
        {
            if(id <= 0)
            {
                return Ok(BaseResult.Result("400","Id有误！"));
            }
            string msg;
            bool result = _postService.UpdateStatus(id, out msg);
            if (result)
            {
                return Ok(BaseResult.Result(true, "发布成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "发布失败！" + msg, "500"));
        }

        /// <summary>
        /// 删除公告信息
        /// </summary>
        /// <param name="postId">公告Id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelPost(long postId)
        {
            bool result = _postService.DelBBS_Post(postId, out string msg);
            if (result)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！" + msg, "500"));
        }

        /// <summary>
        /// Id查询
        /// </summary>
        /// <param name="areaId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPost(long postId)
        {
            var cobj = _postService.GetPost(postId);
            if (cobj != null)
            {
                return Ok(BaseResult.Result(cobj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
    }
}
